﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Drawing;
using System.Drawing.Imaging;
using System.Drawing.Drawing2D;
using System.Runtime.InteropServices;
using System.Threading;

namespace Crayons.Components
{
    public class CanvasPanel : UserControl
    {   
        private Document m_document;

        public CanvasPanel()
        {
            Init();
        }

        protected void Init()
        {
            m_document = new Document(this);
            m_document.ContentSize = new Size(512, 512);
            this.SetStyle(ControlStyles.DoubleBuffer, true);
            this.SetStyle(ControlStyles.UserPaint, true);
            this.SetStyle(ControlStyles.AllPaintingInWmPaint, true);
 
        }

        protected override void OnLoad(EventArgs e)
        {
            m_document.OnLoad();            
            base.OnLoad(e);
        }

        protected override void OnLocationChanged(EventArgs e)
        {
            base.OnLocationChanged(e);
        }

        protected override void OnSizeChanged(EventArgs e)
        {
            m_document.OnSizeChanged(Width, Height);
            base.OnSizeChanged(e);
        }
        
        protected override void OnKeyUp(KeyEventArgs e)
        {
            m_document.OnKeyUp(e);
            base.OnKeyUp(e);
        }

        protected override void OnMouseDown(MouseEventArgs e)
        {
            m_document.OnMouseDown(e);
            base.OnMouseDown(e);
        }

        protected override void OnMouseUp(MouseEventArgs e)
        {
            m_document.OnMouseUp(e);
            base.OnMouseUp(e);
        }

        protected override void OnMouseMove(MouseEventArgs e)
        {
            m_document.OnMouseMove(e);
            base.OnMouseMove(e);
        }

        protected override void OnPaintBackground(PaintEventArgs e)
        {
            //Do nothing
        }

        protected override void OnPaint(PaintEventArgs e)
        {
            e.Graphics.DrawImageUnscaled(m_document.ContentImage, 0, 0);
            base.OnPaint(e);
        }

    }
}
